#!/bin/sh
homedir="$PREFIX/../home"
mcsmanager_install_path="$homedir/mcsmanager"
mcsmanager_donwload_addr="https://gitee.com/SnowSwordScholar/mcsmanager-x-termux-install/releases/download/v10/mcsmanager_linux_release.tar.gz"

echo_red() {
  printf '\033[1;31m%b\033[0m\n' "$@"
}

echo_green() {
  printf '\033[1;32m%b\033[0m\n' "$@"
}

echo_cyan() {
  printf '\033[1;36m%b\033[0m\n' "$@"
}

echo_cyan_n() {
  printf '\033[1;36m%b\033[0m' "$@"
}

echo_yellow() {
  printf '\033[1;33m%b\033[0m\n' "$@"
}


clear


echo_cyan '
 即将执行Mcsmanger的安装
 安装将进行以下操作:
 将Termux的源换为清华源
 执行一次aptupgrade
 安装wget、termux-services、node
 将Mcsmanager注册为系统服务并自动启动
'
echo_red '
修改termux包名可能导致无法正常安装！


将在5s后自动安装
'
sleep 10
clear

# Somwthing about APT
echo_green [🍻]正在换用清华源
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
echo_green [🎶]更新Termux的包
apt update
yes | apt-get upgrade
apt update
#解决update后源文件更改问题
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
pkg update
echo_green [+]开始安装必要依赖
apt install wget termux-services nodejs -y

echo
echo_yellow "=============== Node.JS Version ==============="
echo_yellow " node: $(node -v)"
echo_yellow " npm: v$(/usr/bin/env node $PREFIX/bin/npm -v)"
echo_yellow "=============== Node.JS Version ==============="
echo


# Start to Install MCSM
rm -r ${mcsmanager_install_path}/daemon
rm -r ${mcsmanager_install_path}/web

echo_green [↓]下载相关内容

mkdir -p $mcsmanager_install_path
cd $mcsmanager_install_path
wget --tries=3 $mcsmanager_donwload_addr
tar -zxf mcsmanager_linux_release.tar.gz -o
rm -rf "$mcsmanager_install_path/mcsmanager_linux_release.tar.gz"
# Install daemon
echo_green [↓]编译Daemon
cd daemon
/usr/bin/env node $PREFIX/bin/npm install  --registry=https://registry.npmmirror.com --production > npm_install_log
cd ..
# Install web
echo_green [↓]编译Web
cd web
/usr/bin/env node $PREFIX/bin/npm install  --registry=https://registry.npmmirror.com --production > npm_install_log
cd ..


echo
echo_yellow "=============== MCSManager ==============="
echo_green " Daemon: ${mcsmanager_install_path}/daemon"
echo_green " Web: ${mcsmanager_install_path}/web"
echo_yellow "=============== MCSManager ==============="
echo
echo_green "[+] MCSManager installation success!"


echo_green [+]配置为系统服务

# Daemon
mkdir -p $PREFIX/var/service/mcsm-daemon
cd $PREFIX/var/service/mcsm-daemon
echo "#!$PREFIX/bin/sh
exec 2>&1
cd ${mcsmanager_install_path}/daemon
exec node app.js 2>&1" > run
mkdir -p $PREFIX/var/service/mcsm-daemon/log
cd $PREFIX/var/service/mcsm-daemon/log
echo '#!/data/data/com.termux/files/usr/bin/sh
svlogger="/data/data/com.termux/files/usr/share/termux-services/svlogger"
exec "${svlogger}" "$@"' > run
chmod +x $PREFIX/var/service/mcsm-daemon/run
chmod +x $PREFIX/var/service/mcsm-daemon/log/run

# Web
mkdir -p $PREFIX/var/service/mcsm-web
cd $PREFIX/var/service/mcsm-web
echo "#!$PREFIX/bin/sh
exec 2>&1
cd ${mcsmanager_install_path}/web
exec node app.js 2>&1" > run
mkdir -p $PREFIX/var/service/mcsm-web/log
cd $PREFIX/var/service/mcsm-web/log
echo '#!/data/data/com.termux/files/usr/bin/sh
svlogger="/data/data/com.termux/files/usr/share/termux-services/svlogger"
exec "${svlogger}" "$@"' > run
chmod +x $PREFIX/var/service/mcsm-web/run
chmod +x $PREFIX/var/service/mcsm-web/log/run



    echo_green "安装已完成！欢迎使用 MCSManager 面板！"
    echo_yellow " "
    echo_cyan_n "控制面板地址：   "
    echo_yellow "http://你的公网IP:23333"
    echo_red "你必须开放 23333（面板）和 24444（守护进程用）端口，控制面板需要这两个端口才能正常工作。"
    echo_yellow "（可这是安卓） "
    echo_cyan "下面是常用的几个命令："
    echo_cyan "启动面板 sv up mcsm-{daemon,web}"
    echo_cyan "停止面板 sv down stop mcsm-{daemon,web}"
    echo_cyan "重启面板 sv restart mcsm-{daemon,web}"
    echo_yellow " "
    echo_cyan "官方文档（必读）：https://docs.mcsmanager.com/"
    echo_yellow "=================================================================="
    echo_red "注意：你需要重启Termux来使服务生效！在重启前执行以上命令无效"
